package com.zhisheng.utils;

import com.zhisheng.config.MyJwtProperties;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * 创建者： 智晟全哥
 *
 * 自己抽取一个专门生成和解析令牌的工具类
 *
 *
 */

@Component
public class MyJwtUtils {

    @Autowired
    private MyJwtProperties properties;

    // 写两个方法,一个生成,一个解析
    public String createJwt(Map<String,Object> map){
        // 生成令牌
        String s = Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.valueOf(properties.getSiginType()), properties.getKey())
                .setExpiration(new Date(System.currentTimeMillis() + properties.getTime()))
                .compact();
        return s;
    }

    public Map<String,Object> parseJwt(String jwt){
        Claims body = Jwts.parser()
                .setSigningKey(properties.getKey())
                .parseClaimsJws(jwt)
                .getBody();
        return body;
    }


}
